import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import auth from '../utils/auth'

const store = new Vuex.Store({
  state: {
    logined: false,
    userInfo: {
      token: auth.get()
    },
    // 群聊选择用户
    chatUsers: []
  },
  getters: {
    token: state => {
		return state.userInfo.token
    },
    userInfo: state => {
      return state.userInfo
    },
    logined: state => {
      return state.logined
    },
    chatUsers: state => {
      return state.chatUsers
    }
  },
  mutations: {
    login(state, userInfo) {
      // console.log(userInfo, 'userinfo set')
      state.logined = true
      state.userInfo.token = userInfo.token
	  uni.setStorageSync('isLogined', true)
      auth.save(userInfo.token)
    },
    logout(state) {
      state.logined = false
	  uni.setStorageSync('isLogined', false)
      state.userInfo = {}
      auth.remove()
    },
    setChatUsers(state, userIds) {
      state.chatUsers = userIds
    },
    clearChatUsers(state) {
      state.chatUsers = []
    }
  }
})
export default store
